﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCQuestionList : System.Web.UI.UserControl
	{
		private IQuestionProvider _QuestionProvider;

		/// <summary>
		/// 1: 已解决
		/// 2: 未解决
		/// 3: 我的问题
		/// 4: 我回答的问题
		/// </summary>
		public int GetQuestionType
		{
			get;
			set;
		}

		private IList<Question> _DataSource;
		private int _TotalRecords;

		public IList<Question> DataSourse
		{
			get
			{
				var pageIndex = this.PageIndex;
				var totalRecords = 0;
				var pageSize = this.PageSize;

				var selectedQuestionCat = ddlQuestionCategory.SelectedValue;
				var questionCatID = int.Parse(selectedQuestionCat);

				var user = SiteContext.Current.CurrentUser;

				if (_QuestionProvider == null)
				{
					_QuestionProvider = new QuestionProvider();
				}

				List<Question> data = null;

				switch (GetQuestionType)
				{
					case 1:
						{
							if (user != null && user.Role.RoleName.ToLower().Equals("admin"))
							{
								var drugStore = SiteContext.Current.CurrentDrugStore;

								if (drugStore != null)
								{
									data = _QuestionProvider.GetQuestionByState(QuestionState.Resolved, questionCatID, drugStore.DrugStoreID, pageIndex, pageSize, out totalRecords);
								}
							}
							else
							{
								if (user != null && user.DrugStoreID.GetValueOrDefault(0) > 0)
								{
									data = _QuestionProvider.GetQuestionByState(user.UserID, QuestionState.Resolved, questionCatID, user.DrugStoreID.Value, pageIndex, pageSize, out totalRecords);
								}
							}
						}//已解决
						break;
					case 2:
						{
							if (user != null && user.Role.RoleName.ToLower().Equals("admin"))
							{
								var drugStore = SiteContext.Current.CurrentDrugStore;

								if (drugStore != null)
								{
									data = _QuestionProvider.GetQuestionByState(QuestionState.UnResolved, questionCatID, drugStore.DrugStoreID, pageIndex, pageSize, out totalRecords);
								}
							}
							else
							{
								if (user != null && user.DrugStoreID.GetValueOrDefault(0) > 0)
								{
									data = _QuestionProvider.GetQuestionByState(user.UserID, QuestionState.UnResolved, questionCatID, user.DrugStoreID.Value, pageIndex, pageSize, out totalRecords);
								}
							}
						}//未解决
						break;
					case 3:
						{
							if (user != null)
							{
								data = _QuestionProvider.GetQuestionByUser(user.UserID, questionCatID, pageIndex, pageSize, out totalRecords);
							}
						}//我的问题
						break;
					case 4:
						{
							if (user != null)
							{
								data = _QuestionProvider.GetHaveAnsweredQuestionByUser(user.UserID, questionCatID, pageIndex, pageSize, out totalRecords);
							}
						}//我回答的问题
						break;
				}

				_DataSource = data;

				this.TotalRecords = totalRecords;

				return _DataSource;
			}
		}

		public int PageIndex
		{
			get
			{
				if (ViewState["PageIndex"] == null)
				{
					return 0;
				}
				else
				{
					return int.Parse(ViewState["PageIndex"].ToString());
				}
			}
			set
			{
				ViewState["PageIndex"] = value;
			}
		}

		public int TotalRecords
		{
			get
			{
				return _TotalRecords;
			}
			set
			{
				_TotalRecords = value;
			}
		}

		public int TotalPages
		{
			get
			{
				var data = _DataSource == null ? this.DataSourse : _DataSource;

				if (data == null)
				{
					return 0;
				}

				int totalRecords = TotalRecords;

				int totalPages = 0;

				if (totalRecords <= PageSize)
				{
					totalPages = 1;
				}

				if (totalRecords % PageSize == 0)
				{
					totalPages = totalRecords / PageSize;
				}

				if (totalRecords % PageSize > 0)
				{
					totalPages = totalRecords / PageSize + 1;
				}

				return totalPages;
			}
		}

		public int PageSize
		{
			get
			{
				return int.Parse(ddlPageSize.SelectedValue);
			}
		}

		public string RoleName
		{
			get
			{
				var u = SiteContext.Current.CurrentUser;

				var result = string.Empty;

				if(u != null)
				{
					result = u.Role.RoleName;
				}

				return result;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindQuestionCategory();
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			BindQuestion();

			var currentPageIndex = this.PageIndex;
			var totalPages = TotalPages;

			// set pager index links
			List<PagerIndex> indexs = new List<PagerIndex>();

			if (totalPages <= 25)
			{
				for (int i = 1; i <= totalPages; i++)
				{
					indexs.Add(new PagerIndex(i.ToString()));
				}
			}
			else
			{
				for (var i = 13; i > 0; i--)
				{
					if (indexs.Count >= 27)
					{
						break;
					}

					var index = currentPageIndex - i;

					if (index > 0)
					{
						indexs.Add(new PagerIndex(index.ToString()));
					}
				}

				for (var i = 0; i <= 13; i++)
				{
					if (indexs.Count > 27)
					{
						break;
					}

					var index = currentPageIndex + i;

					if (index <= totalPages && index > 0)
					{
						indexs.Add(new PagerIndex(index.ToString()));
					}
				}
			}

			this.RptrPageIndex.DataSource = indexs;
			this.RptrPageIndex.DataBind();

			this.LabelTotalPages.Text = totalPages.ToString();
			this.LabelTotalRecords.Text = TotalRecords.ToString();

			// disenabel the current pagerindex link
			if (RptrPageIndex.Items.Count > 0)
			{
				for (int i = 0; i < RptrPageIndex.Items.Count; i++)
				{
					LinkButton thePagerIndexLink = RptrPageIndex.Items[i].FindControl("LBPageIndex") as LinkButton;

					if (thePagerIndexLink.Text.Trim().Equals((currentPageIndex + 1).ToString()))
					{
						thePagerIndexLink.Enabled = false;
					}
				}
			}

			lbtnFirstPage.Enabled = (currentPageIndex > 0);

			lbtnLastPage.Enabled = totalPages > 1 && (currentPageIndex != totalPages - 1);
		}

		protected void gvQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.DataItem != null)
			{
				var dataItem = e.Row.DataItem as Question;

				var hlQuestion = e.Row.FindControl("hlQuestion") as HyperLink;

				hlQuestion.Text = dataItem.QuestionContent;
				hlQuestion.NavigateUrl = "~/QuestionDetail.aspx?questionid=" + dataItem.QuestionID.ToString();

				if(this.GetQuestionType == 3 || (this.GetQuestionType == 2 && (RoleName == "storeadmin" || RoleName == "admin")))
				{
					var lbtnDelete = e.Row.FindControl("lbtnDelete") as LinkButton;

					lbtnDelete.Text = "删除";
					lbtnDelete.CommandArgument = dataItem.QuestionID.ToString();
				}
			}
		}

		protected void ddlQuestionCategory_SelectedIndexChanged(object sender, EventArgs e)
		{
			//BindQuestion();
		}

		protected void gvQuestion_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "remove")
			{
				var id = int.Parse(e.CommandArgument.ToString());

				if (_QuestionProvider == null)
				{
					_QuestionProvider = new QuestionProvider();
				}

				if (_QuestionProvider.DeleteQuestion(id))
				{
					//BindQuestion();
				}
			}
		}

		protected void LBPageIndex_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			switch (e.CommandName)
			{
				case "PageIndex":
					{
						LinkButton theLink = sender as LinkButton;

						int index = int.Parse(theLink.Text);

						PageIndex = index - 1;

						//TotalRecords = DataSource.Count();

						BindQuestion();
					}
					break;
			}
		}

		protected void lbtnFirstPage_Click(object sender, EventArgs e)
		{
			PageIndex = 0;
			BindQuestion();
		}

		protected void lbtnLastPage_Click(object sender, EventArgs e)
		{
			PageIndex = TotalPages - 1;
			BindQuestion();
		}

		protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.PageIndex = 0;
		}

		private void BindQuestionCategory()
		{
			if (_QuestionProvider == null)
			{
				_QuestionProvider = new QuestionProvider();
			}

			ddlQuestionCategory.Items.Clear();
			ddlQuestionCategory.DataSource = _QuestionProvider.GetAllQuestionCategory();
			ddlQuestionCategory.DataBind();

			ddlQuestionCategory.Items.Insert(0, new ListItem("所有类别", "0"));
			ddlQuestionCategory.SelectedIndex = 0;
		}

		private void BindQuestion()
		{
			var data = this.DataSourse;

			gvQuestion.DataSource = data;
			gvQuestion.DataBind();
		}
	}
}